package com.hellobike.haxing.modules.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hellobike.haxing.modules.product.entity.PromotionGroup;
import com.hellobike.haxing.modules.product.model.PromotionGroupNoJoin;
import com.hellobike.haxing.modules.product.model.PromotionGroupNojoinSku;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description: 拼团商品
 * @Author: haxing-boot
 * @Date: 2020-01-29
 * @Version: V1.0
 */
public interface PromotionGroupMapper extends BaseMapper<PromotionGroup> {


    @Select("select a.title,a.id as product_id,b.id ,a.spu_code from t_product a \n" +
            "left join t_promotion_group b \n" +
            "on   a.id = b.product_id and b.deleted=0 AND b.promotion_id=#{id}\n" +
            "where  a.id = #{productId} and a.deleted=0 order by a.id desc limit 1")
    PromotionGroupNoJoin selectPromotionGroupNoJoin(@Param("id") Long id,
                                                    @Param("productId") Long productId);

    @Select("select m.group_price,a.price,m.stock_init,m.stock_left,a.barcode,a.dimension_values,a.cogs_price,a.id as sku_id,m.user_buy_limit from t_sku a \n" +
            "left join (select * from  t_promotion_group_sku b \n" +
            "join t_promotion_group c on c.id = b.group_id and c.promotion_id = #{id} ) m on   a.id = m.sku_id  \n" +
            "where a.spu_id = #{productId} ")
    List<PromotionGroupNojoinSku> selectPromotionGroupSkuNojoin(@Param("id") Long id, @Param("productId") Long productId);

    @Select("select a.title,a.id as product_id,b.id ,a.spu_code from t_product a \n" +
            "join t_promotion_group b on a.id = b.product_id and a.deleted=0 and b.deleted=0\n" +
            "and b.promotion_id = #{id}  and b.status=1 order by a.id desc limit 1")
    PromotionGroupNoJoin selectPromotionGroupNoJoinProduct(@Param("id") Long promotionId);
}
